export const initErd = (data) => {
    console.log("initErd", data);


    let tableMap = {}
    let tableSet = []

    data.forEach(element => {

        if (!tableMap[element.table]) {
            tableMap[element.table] = []
        }

        tableMap[element.table].push(element)


    });


    Object.entries(tableMap).forEach(([key, value]) => {
        tableSet.push({
            key: key,
            nodeType: "Node",
            fieldZhName: key,
            items: value.map(item => {
                return {
                    fieldCnName: item.column,
                    nodeType: "Node",
                    fieldZhName: item.description,
                    fieldIndexes: '',
                    iskey: true,
                    figure: ['uid', 'create_time', 'update_time'].includes(item.column) ? 'Circle' : 'Decision',
                    color: ['uid', 'create_time', 'update_time'].includes(item.column) ? '#52ce60' : '#be4b15',
                }
            })
        })
    });

    console.log("tableMap", tableMap);

    let $ = window.go.GraphObject.make; // for conciseness in defining templates
    let myDiagram = $(
        window.go.Diagram,
        "myDiagramDiv", // id挂载dome节点
        {
            allowDelete: true,
            allowCopy: true,
            layout: $(window.go.ForceDirectedLayout),
            "undoManager.isEnabled": true,
            // ModelChanged: onModelChanged,
            "toolManager.mouseWheelBehavior": window.go.ToolManager.WheelZoom, //有⿏标滚轮事件放⼤和缩⼩
        }
    );

    let itemTempl = $(
        window.go.Panel,
        "Horizontal",
        $(
            window.go.Shape, {
                desiredSize: new window.go.Size(15, 15),
                strokeJoin: "round",
                strokeWidth: 3,
                stroke: null,
                margin: 2,
            },
            new window.go.Binding("figure", "figure"),
            new window.go.Binding("fill", "color"),
            new window.go.Binding("stroke", "color")
        ),
        $(
            window.go.TextBlock, {
                stroke: "#333333",
                font: "bold 14px sans-serif",
            },
            new window.go.Binding("text", "fieldCnName"), {
                click: function (e, obj) {
                    // myDiagram.commandHandler.deleteSelection();
                },
                doubleClick: function (e, obj) {
                    if (obj.part.data.key == undefined) {
                        currentSelect.selectLink = obj.part.data;
                        currentSelect.selectType = obj.part.data.nodeType;
                        console.log("线段", currentSelect.selectLink);
                        drawer.drawerShow = true;
                    } else {
                        currentSelect.selectNode = obj.part.data;
                        currentSelect.selectType = obj.part.data.nodeType;
                        console.log("节点", currentSelect.selectNode);
                        drawer.drawerShow = true;
                    }
                },
            }
        ),
        $(
            window.go.TextBlock, {
                row: 0,
                alignment: window.go.Spot.BottomRight,
                margin: 5
            },
            new window.go.Binding("text", "fieldZhName")
        )
    );

    // define the Node template, representing an entity
    myDiagram.nodeTemplate = $(
        window.go.Node,
        "Auto", // the whole node panel
        {
            selectionAdorned: true,
            resizable: true,
            layoutConditions: window.go.Part.LayoutStandard & ~window.go.Part.LayoutNodeSized,
            fromSpot: window.go.Spot.AllSides,
            toSpot: window.go.Spot.AllSides,
            isShadowed: true,
            shadowOffset: new window.go.Point(1, 1),
            shadowColor: "#C5C1AA",
            scale: 1,
        },
        new window.go.Binding("location", "location").makeTwoWay(),
        // whenever the PanelExpanderButton changes the visible property of the "LIST" panel,
        // clear out any desiredSize set by the ResizingTool.
        new window.go.Binding("desiredSize", "visible", function (v) {
            return new window.go.Size(NaN, NaN);
        }).ofObject("LIST"),
        // define the node's outer shape, which will surround the Table
        $(window.go.Shape, "RoundedRectangle", {
            fill: "white",
            stroke: "#eeeeee",
            strokeWidth: 3,
        }),
        $(
            window.go.Panel,
            "Table", {
                margin: 8,
                stretch: window.go.GraphObject.Fill
            },
            $(window.go.RowColumnDefinition, {
                row: 0,
                sizing: window.go.RowColumnDefinition.None,
            }),
            // the table header
            $(
                window.go.TextBlock, {
                    row: 0,
                    alignment: window.go.Spot.Center,
                    margin: new window.go.Margin(0, 24, 0, 2), // leave room for Button
                    font: "bold 16px sans-serif",
                },
                new window.go.Binding("text", "key")
            ),
            // the collapse/expand button
            $(
                "PanelExpanderButton",
                "LIST", // the name of the element whose visibility this button toggles
                {
                    row: 0,
                    alignment: window.go.Spot.TopRight
                }
            ),
            // the list of Panels, each showing an attribute
            $(
                window.go.Panel,
                "Vertical", {
                    name: "LIST",
                    row: 1,
                    padding: 2,
                    alignment: window.go.Spot.TopLeft,
                    defaultAlignment: window.go.Spot.Left,
                    stretch: window.go.GraphObject.Horizontal,
                    itemTemplate: itemTempl,
                },
                new window.go.Binding("itemArray", "items")
            )
        ), // end Table Panel
        $(
            window.go.TextBlock, {
                row: 0,
                alignment: window.go.Spot.BottomRight
            },
            new window.go.Binding("text", "fieldZhName")
        )
    ); // end Node
    myDiagram.linkTemplate = $(
        window.go.Link, // the whole link panel
        {
            selectionAdorned: true,
            layerName: "Foreground",
            reshapable: true,
            routing: window.go.Link.AvoidsNodes,
            corner: 5,
            curve: window.go.Link.JumpOver,
        },
        $(
            window.go.Shape, // the link shape
            {
                stroke: "#303B45",
                strokeWidth: 2.5
            }
        ),
        $(
            window.go.TextBlock, // the "from" label
            {
                textAlign: "center",
                font: "bold 11px sans-serif",
                stroke: "#1967B3",
                segmentIndex: 0,
                segmentOffset: new window.go.Point(NaN, NaN),
                scale: 1,
            },
            new window.go.Binding("text", "text")
        ),
        $(
            window.go.TextBlock, // the "to" label
            {
                textAlign: "center",
                font: "bold 11px sans-serif",
                stroke: "#1967B3",
                segmentIndex: -1,
                segmentOffset: new window.go.Point(NaN, NaN),
                scale: 1,
            },
            new window.go.Binding("text", "toText")
        )
    );

    myDiagram.model = $(window.go.GraphLinksModel, {
        copiesArrays: true,
        copiesArrayObjects: true,
        nodeDataArray: tableSet,
    });
}